package com.ws.lv.service.impl;

import com.ws.lv.bean.Acl_permission;
import com.ws.lv.mapper.Acl_permissionMapper;
import com.ws.lv.service.Acl_permissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.ArrayList;
import java.util.Collections;
import java.util.List;
import java.util.stream.Collectors;

@Service
public class Acl_permissionServiceImpl implements Acl_permissionService {

    @Autowired
    Acl_permissionMapper acl_permissionMapper;

    @Override
    public List<Acl_permission> selectAcl_permissionAll() {
        return acl_permissionMapper.selectAcl_permissionAll();
    }



    @Override
    public List<Acl_permission> selectAcl_permissions(String name, String path) {
        //获取所有权限
        List<Acl_permission> acl_permissions = acl_permissionMapper.selectAcl_permissions(name, path);
        //改造
        List<Acl_permission> newList = refactor(acl_permissions);
        return newList;
    }

    public List<Acl_permission> refactor(List<Acl_permission> list){
        List<Acl_permission> newList = new ArrayList<>();
        //获取父级节点放入集合
        list.stream().forEach(acl_permission -> {
            if(acl_permission.getPid() == 0){
                List<Acl_permission> collect = list.stream().filter(ap -> ap.getPid() == acl_permission.getPerm_id()).collect(Collectors.toList());
                acl_permission.setChildren(collect);
                newList.add(acl_permission);
            }
        });
        return newList;
    }

    @Override
    public int deleteAcl_permission(Integer perm_id) {
        return acl_permissionMapper.deleteAcl_permission(perm_id);
    }

    @Override
    public Acl_permission selectAcl_permissionById(Integer perm_id) {
        return acl_permissionMapper.selectAcl_permissionById(perm_id);
    }

    @Override
    public List<Acl_permission> selectAcl_permissionByPid(Integer pid) {
        return acl_permissionMapper.selectAcl_permissionByPid(pid);
    }

    @Override
    public int insertAcl_permissionParent(Acl_permission acl_permission) {
        return acl_permissionMapper.insertAcl_permissionParent(acl_permission);
    }

    @Override
    public int insertAcl_permissionChild(Acl_permission acl_permission) {
        return acl_permissionMapper.insertAcl_permissionChild(acl_permission);
    }

    @Override
    public int updateAcl_permission(Acl_permission acl_permission) {
        return acl_permissionMapper.updateAcl_permission(acl_permission);
    }

    @Override
    public int deleteAcl_role_permissionByPermId(Integer perm_id) {
        return acl_permissionMapper.deleteAcl_role_permissionByPermId(perm_id);
    }

}
